'''
crawler script
'''
from common.config import *
import time
from pprint import pprint
from db.mongo import MongoClient
class Spider( object ):
    def __init__(self):
        # 实例化mongo数据库
        self.mongo = MongoClient()

    # 解析函数
    def parseJson(self,res=None):
        for item in res["data"]:
            data = {
                # 新闻标题
                "title":item["title"] if "title" in item else "",
                # 新闻简介
                "abstract":item["abstract"] if "abstract" in item else "",
                # 新闻类型
                "tag":item["chinese_tag"] if "chinese_tag" in item else "",
                #新闻源
                "source":item["source"] if "source" in item else "",
                #评论数量
                "comments_count":item["comments_count"] if "comments_count" in item else "" ,
                #原文链接
                "article_url": urls["domain"] + "/a" + item["group_id"] if "group_id" in item else "",
                #发布时间
                "release_time":time.strftime("%Y/%m/%d %H:%M:%S",time.localtime( item["behot_time"])) if "behot_time" in item else ""

            }
            pprint( data )
            # 存储到mongo数据库
            self.mongo.saveMongo( data )

